from PyQt6.QtCore import Qt, pyqtSlot
from PyQt6.QtGui import QDoubleValidator
from PyQt6.QtWidgets import QApplication, QGridLayout
from qfluentwidgets import (CardWidget, BodyLabel, SubtitleLabel, LargeTitleLabel, LineEdit, CaptionLabel)
import sys
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP

class CalculatorCard(CardWidget):
    """卡片"""
    def __init__(self, parent=None):
        super().__init__(parent)
        
        # 开赛时间
        self.__kick_off_time = BodyLabel()
        self.__kick_off_time.setAlignment(Qt.AlignmentFlag.AlignLeft)
        # 联赛
        self.__league = BodyLabel()
        self.__league.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        # 主队
        self.__team_home = SubtitleLabel()
        self.__team_home.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # 主队盘口
        self.__handicap_home = BodyLabel()
        self.__handicap_home.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # 主队赔率
        self.__odds_home = SubtitleLabel()
        self.__odds_home.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # 主队来源
        self.__source_home = BodyLabel()
        self.__source_home.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        # 客队
        self.__team_away = SubtitleLabel()
        self.__team_away.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # 客队盘口
        self.__handicap_away = BodyLabel()
        self.__handicap_away.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # 客队赔率
        self.__odds_away = SubtitleLabel()
        self.__odds_away.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # 客队来源
        self.__source_away = BodyLabel()
        self.__source_away.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        # 利润百分百
        self.__water_level = LargeTitleLabel()
        self.__water_level.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # 总成本
        self.__cost_line = SubtitleLabel()
        self.__cost_line.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # 盈利金额
        self.__profit_amount = BodyLabel()
        self.__profit_amount.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        # 主队成本
        self.__cost_line_home = LineEdit()
        self.__cost_line_home.setValidator(QDoubleValidator())    # 设置浮点数验证器
        self.__cost_line_home.textChanged.connect(self.calculate_home)
        # 客队成本
        self.__cost_line_away = LineEdit()
        self.__cost_line_away.setValidator(QDoubleValidator())    # 设置浮点数验证器
        self.__cost_line_away.textChanged.connect(self.calculate_away)
        
        self.calculate_flag = False     # 计算标志变量，用于防止死循环
        self.ui_flag = True             # 设置ui标志变量，用于防止死循环
        
        # self.setup_ui()

    def setup_ui(self):
        """设置ui"""
        grid_layout = QGridLayout()
        grid_layout.setContentsMargins(0, 0, 0, 0)
        grid_layout.setSpacing(0)
        
        grid_layout.addWidget(self.__kick_off_time, 0, 0, 1, 2) # 开赛时间
        grid_layout.addWidget(self.__league, 0, 2) # 联赛
        
        # 主队
        grid_layout.addWidget(self.__team_home, 1, 0)
        grid_layout.addWidget(self.__handicap_home, 2, 0)
        grid_layout.addWidget(self.__odds_home, 3, 0)
        grid_layout.addWidget(self.__source_home, 4, 0)
        # 客队
        grid_layout.addWidget(self.__team_away, 1, 2)
        grid_layout.addWidget(self.__handicap_away, 2, 2)
        grid_layout.addWidget(self.__odds_away, 3, 2)
        grid_layout.addWidget(self.__source_away, 4, 2)
        
        grid_layout.addWidget(self.__water_level, 2, 1, 2, 1)  # 利润百分百
        
        grid_layout.addWidget(self.__cost_line_home, 5, 0)  # 主队成本
        grid_layout.addWidget(self.__cost_line_away, 5, 2)  # 客队成本
        grid_layout.addWidget(self.__cost_line, 5, 1)       # 总成本
        grid_layout.addWidget(self.__profit_amount, 4, 1)   # 盈利金额
        
        self.setLayout(grid_layout)
        self.setStyleSheet("padding: 0px;")
        # self.setStyleSheet("""
        #     padding: 0px;   /* 设置控件的内边框距离 */
        #     background-color: transparent; /* 设置透明背景 */
        #     border: none; /* 去掉边框 */
        # """)

    def calculate_home(self):
        """主队成本输入计算客队金额"""
        if self.can_convert_to_decimal(self.__cost_line_home.text()):
            if not self.calculate_flag:
                self.calculate_flag = True
                cost_home = Decimal(self.__cost_line_home.text()) # 获取主队成本
                odds_home = Decimal(self.__odds_home.text()) # 获取主队赔率
                odds_away = Decimal(self.__odds_away.text()) # 获取客队赔率
                amount = cost_home * odds_home / odds_away
                rounded_amount = amount.quantize(Decimal("0"), rounding=ROUND_HALF_UP)
                self.__cost_line_away.setText(str(rounded_amount))
                # 计算总成本
                cost_away = Decimal(self.__cost_line_away.text()) # 获取客队成本
                cost_line = cost_home + cost_away
                rounded_cost_line = cost_line.quantize(Decimal("0"), rounding=ROUND_HALF_UP)
                self.__cost_line.setText(str(rounded_cost_line))
                # 计算利润
                profit_amount = cost_home * odds_home - rounded_cost_line
                rounded_profit_amount = profit_amount.quantize(Decimal("0"), rounding=ROUND_HALF_UP)
                self.__profit_amount.setText(str(rounded_profit_amount))
                self.calculate_flag = False

    def calculate_away(self):
        """客队成本输入计算主队金额"""
        if self.can_convert_to_decimal(self.__cost_line_away.text()):
            if not self.calculate_flag:
                self.calculate_flag = True
                cost_away = Decimal(self.__cost_line_away.text()) # 获取客队成本
                odds_away = Decimal(self.__odds_away.text()) # 获取客队赔率
                odds_home = Decimal(self.__odds_home.text()) # 获取主队赔率
                amount = cost_away * odds_away / odds_home
                rounded_amount = amount.quantize(Decimal("0"), rounding=ROUND_HALF_UP)
                self.__cost_line_home.setText(str(rounded_amount))
                # 计算总成本
                cost_home = Decimal(self.__cost_line_home.text()) # 获取主队成本
                cost_line = cost_home + cost_away
                rounded_cost_line = cost_line.quantize(Decimal("0"), rounding=ROUND_HALF_UP)
                self.__cost_line.setText(str(rounded_cost_line))
                # 计算利润
                profit_amount = cost_away * odds_away - rounded_cost_line
                rounded_profit_amount = profit_amount.quantize(Decimal("0"), rounding=ROUND_HALF_UP)
                self.__profit_amount.setText(str(rounded_profit_amount))
                self.calculate_flag = False

    def can_convert_to_decimal(self, value):
        """判断字符串是否可以转换为Decimal类型"""
        try:
            Decimal(value)
            return True
        except InvalidOperation:
            return False

    @pyqtSlot(object)
    def receive_bottom_signal(self, item):
        """接收football中底部页面(BottomWidget)发来的信号"""
        # 处理子组件发出的信号
        if self.ui_flag:
            self.setup_ui()
            self.ui_flag = False
        # 开赛时间
        self.__kick_off_time.setText(*item.kick_off_time)
        self.__kick_off_time.setStyleSheet("background-color: rgb(212,234,238); border-radius: 1px;")
        # 联赛
        self.__league.setText(*item.league)
        
        # 主队
        self.__team_home.setText(*item.team_home)
        # 主队盘口
        self.__handicap_home.setText(*item.handicap_home)
        # 主队赔率
        self.__odds_home.setText(*item.odds_home)
        # 主队来源
        self.__source_home.setText(*item.source_home)
        
        # 客队
        self.__team_away.setText(*item.team_away)
        # 客队盘口
        self.__handicap_away.setText(*item.handicap_away)
        # 客队赔率
        self.__odds_away.setText(*item.odds_away)
        # 客队来源
        self.__source_away.setText(*item.source_away)
        # 利润百分百
        self.__water_level.setText(*item.water_level)

if __name__ == '__main__':
    data = {
        "kick_off_time": "",
        "league": "",
        "team_home": "",
        "handicap_home": "",
        "odds_home": "",
        "source_home": "",
        "team_away": "",
        "handicap_away": "",
        "odds_away": "",
        "source_away": "",
        "water_level": "",
        "icon": ""
    }
    app = QApplication(sys.argv)
    window = CalculatorCard(data)
    window.show()
    sys.exit(app.exec())